﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO.BusinessObjectRules
{

         [TestFixture]
    public class TestDMRelationshipOneSideMustBeSingle
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        [Test]
        public void Test_CreateDMRelationshipOneSideMustBeSingleRule()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship relationship = dmClass.OwnerRelationships.CreateBusinessObject();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMRelationshipOneSideMustBeSingle();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMRelationshipOneSideMustBeSingle", rule.Name);
            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);
        }


//        [Test]
//        public void Test_DMRelationshipOneSideMustBeSingleRule_WhenNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMRelationshipOneSideMustBeSingle(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//                //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmRelationship", ex.ParamName);
//            }
//        } 

        [Test]
        public void Test_DMRelationshipOneSideMustBeSingleRule_WhenReverseRelationNull_ShouldRetTrue()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship relationship = dmClass.OwnerRelationships.CreateBusinessObject();
            relationship.ReverseRelationship = null;
            var rule = new DMRelationshipOneSideMustBeSingle();
            //---------------Assert Precondition----------------
            Assert.IsNull(relationship.ReverseRelationship);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(relationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid, "Although the object is in an invalid state you should return true since the compulsory reverse rel rule should be validated elsewhere");
        }

        [Test]
        public void Test_IsValid_WhenReverseSingle_ShouldPass()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = new DMRelationship();
            IBusinessObjectRule rule = new DMRelationshipOneSideMustBeSingle();
            dmRelationship.Cardinality = Cardinality.Multiple;
            dmRelationship.ReverseRelationship.Cardinality = Cardinality.Single;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }
        [Test]
        public void Test_IsValid_WhenRelationshipSingle_ShouldPass()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = new DMRelationship();
            IBusinessObjectRule rule = new DMRelationshipOneSideMustBeSingle();
            dmRelationship.Cardinality = Cardinality.Single;
            dmRelationship.ReverseRelationship.Cardinality = Cardinality.Multiple;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }
        
        [Test]
        public void Test_IsValid_WhenBothRelationshipMultiple_ShouldFail()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = new DMRelationship();
            IBusinessObjectRule rule = new DMRelationshipOneSideMustBeSingle();
            dmRelationship.Cardinality = Cardinality.Multiple;
            dmRelationship.ReverseRelationship.Cardinality = Cardinality.Multiple;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("One side of the relationship '", rule.Message);
            StringAssert.Contains("'" + dmRelationship + "-" + dmRelationship.ReverseRelationship + "'", rule.Message);
            StringAssert.Contains("' on Classes '" + dmRelationship.ClassDM + "-" + dmRelationship.RelatedClass + "'", rule.Message);
            StringAssert.Contains("must be Single", rule.Message);
        }

        [Test]
        public void Test_IsValid_WhenNoReverseAndRelSingle_ShouldPass()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = new DMRelationship();
            IBusinessObjectRule rule = new DMRelationshipOneSideMustBeSingle();
            dmRelationship.Cardinality = Cardinality.Single;
            dmRelationship.ReverseRelationship = null;
            //---------------Assert Precondition----------------
            Assert.IsNull(dmRelationship.ReverseRelationship);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }


    }
}
